package com.starbaba.loanuser.view;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.loanuser.model.LoanUserInfo;
import com.starbaba.loanuser.service.LoanUserService;
import com.starbaba.loanuser.service.impl.LoanUserServiceImpl;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@SuppressWarnings({"rawtypes","unchecked"})
public class LoanUserAction extends BaseAction implements ModelDriven<LoanUserInfo>{

	private static final long serialVersionUID = 1L;
	
	private LoanUserInfo model;
	private String fileName;
	private InputStream input;

	public String getFileName() {
		return fileName;
	}
	
	public InputStream getInput() {
		return input;
	}
	
	LoanUserService loanUserService = new LoanUserServiceImpl();
	
	/**
	 * 贷款用户信息列表
	 */
	public void getLoanUserList() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		
		List<LoanUserInfo> list = loanUserService.getLoanUserList(pageRequest, pageResponse, paramsMap);

		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());

		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 贷款用户信息
	 */
	public String getLoanUserInfo() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		model = loanUserService.getLoanUserInfo(paramsMap);
		paramsMap.put("model", model);
		return SUCCESS;
	}
	
	/**
	 * Export贷款用户信息列表
	 * */
	public String exportDetailCVS() throws UnsupportedEncodingException {
		
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
        pageRequest.setIDisplayStart(-1);
        pageRequest.setIDisplayLength(-1);
        pageRequest.setOrderby("createtime");
        pageRequest.setOrder("desc");
		List<LoanUserInfo> list =  loanUserService.getLoanUserList(pageRequest, pageResponse, paramsMap);
		String value = loanUserService.exportLoanUserCVS(list);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}


	@Override
	public LoanUserInfo getModel() {
		if (model == null) {
			model = new LoanUserInfo();
		}
		return model;
	}

}
